//
//  SegmentView.m
//  鼎立华服
//
//  Created by 中企互联 on 2017/12/13.
//  Copyright © 2017年 中企互联. All rights reserved.
//

#import "SegmentView.h"

@interface SegmentView ()<UIScrollViewDelegate>
/**
 *  configuration.
 */
{
    CGFloat _HeaderH;
    UIColor *_titleColor;
    UIColor *_titleSelectedColor;
    SegmentStyle _SegmentStyle;
    CGFloat _titleFont;
}
/**
 *  The bottom red slider.
 */
@property(nonatomic,weak)UIView *slider;

@property(nonatomic,strong)NSMutableArray *titleWidthArray;

@property(nonatomic,weak)UIButton *selectedBtn;

@end

@implementation SegmentView

#pragma mark - delayLoading
- (NSMutableArray *)titleWidthArray {
    if (!_titleWidthArray) {
        _titleWidthArray = [NSMutableArray new];
    }
    return _titleWidthArray;
}

#pragma mark - Initialization
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.bounces = NO;
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
//        self.backgroundColor = [UIColor whiteColor];
        
        _HeaderH = frame.size.height;
        _SegmentStyle = SegmentStyleSlider;
        _titleColor = [UIColor darkTextColor];
        _titleSelectedColor = [UIColor blackColor];
        _titleFont = SYRealValue(30 / 2);
    }
    return self;
}

- (void)setTitleArray:(NSArray<NSString *> *)titleArray {
    [self setTitleArray:titleArray withStyle:0];
}

- (void)setTitleArray:(NSArray<NSString *> *)titleArray withStyle:(SegmentStyle)style {
    [self setTitleArray:titleArray titleFont:0 titleColor:nil titleSelectedColor:nil withStyle:style];
}

- (void)setTitleArray:(NSArray<NSString *> *)titleArray
            titleFont:(CGFloat)font
           titleColor:(UIColor *)titleColor
   titleSelectedColor:(UIColor *)selectedColor
            withStyle:(SegmentStyle)style {
    
    //    set style
    if (style != 0) {
        _SegmentStyle = style;
    }
    if (font != 0) {
        _titleFont = font;
    }
    if (titleColor) {
        _titleColor = titleColor;
    }
    if (selectedColor) {
        _titleSelectedColor = selectedColor;
    }
    
    UIView *slider = [[UIView alloc]init];
    slider.frame = CGRectMake(0, _HeaderH-2, SYRealValue(30 / 2), SYRealValue(6 / 2));
    slider.backgroundColor = _titleSelectedColor;
    [self addSubview:slider];
    self.slider = slider;
    
    [self.titleWidthArray removeAllObjects];
    if (style == SegmentStyleZoom) {
        CGFloat totalWidth = SYRealValue(15);
        CGFloat btnSpace = SYRealValue(15);
        for (NSInteger i = 0; i < titleArray.count; i++) {
            //        cache title width
            CGFloat titleWidth = [self widthOfTitle:titleArray[i] titleFont:_titleFont];
            [self.titleWidthArray addObject:[NSNumber numberWithFloat:titleWidth]];
            //        creat button
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [self addSubview:btn];
            CGFloat btnW = titleWidth + SYRealValue(20 / 2);
            btn.frame =  CGRectMake(totalWidth, 0.5, btnW, _HeaderH - 0.5 - 2);
            btn.contentMode = UIViewContentModeCenter;
            btn.titleLabel.textAlignment = NSTextAlignmentCenter;
            btn.tag = i;
            [btn setTitle:titleArray[i] forState:UIControlStateNormal];
            [btn setTitleColor:_titleColor forState:UIControlStateNormal];
            [btn setTitleColor:_titleSelectedColor forState:UIControlStateSelected];
            [btn.titleLabel setFont:[UIFont systemFontOfSize:_titleFont]];
            [btn addTarget:self action:@selector(titleButtonSelected:) forControlEvents:UIControlEventTouchUpInside];
            totalWidth = totalWidth + btnW + btnSpace;
            
            if (i == 0) {
                btn.selected = YES;
                self.selectedBtn = btn;
                self.slider.Width = SYRealValue(30 / 2);
                self.slider.CenterX = btn.CenterX;
                self.selectedBtn.transform = CGAffineTransformMakeScale(1.1, 1.1);
                btn.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(32 / 2)];
            }
        }
        totalWidth = totalWidth+btnSpace;
        self.contentSize = CGSizeMake(totalWidth-SYRealValue(15), 0);
    }else{
        CGFloat totalWidth = SYRealValue(15);
        CGFloat btnSpace = SYRealValue(15);
        for (NSInteger i = 0; i < titleArray.count; i++) {
            //        cache title width
            CGFloat titleWidth = [self widthOfTitle:titleArray[i] titleFont:_titleFont];
            [self.titleWidthArray addObject:[NSNumber numberWithFloat:titleWidth]];
            //        creat button
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [self addSubview:btn];
            CGFloat btnW = titleWidth + SYRealValue(10 / 2);
            btn.frame =  CGRectMake(totalWidth, 0.5, btnW, _HeaderH - 0.5 - 2);
            btn.contentMode = UIViewContentModeCenter;
            btn.titleLabel.textAlignment = NSTextAlignmentCenter;
            btn.tag = i;
            [btn setTitle:titleArray[i] forState:UIControlStateNormal];
            [btn setTitleColor:_titleColor forState:UIControlStateNormal];
            [btn setTitleColor:_titleSelectedColor forState:UIControlStateSelected];
            [btn.titleLabel setFont:[UIFont systemFontOfSize:_titleFont]];
            [btn addTarget:self action:@selector(titleButtonSelected:) forControlEvents:UIControlEventTouchUpInside];
            totalWidth = totalWidth + btnW + btnSpace;
            
            if (i == self.SegmentSelected) {
                btn.selected = YES;
                self.selectedBtn = btn;
                self.slider.Width = SYRealValue(30 / 2);
                self.slider.CenterX = btn.CenterX;
                self.selectedBtn.transform = CGAffineTransformMakeScale(1.1, 1.1);
                btn.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(32 / 2)];
            }
        }
        self.contentSize = CGSizeMake(ScreenWidth, 0);
    }
}

//  button click
- (void)titleButtonSelected:(UIButton *)btn {
    self.selectedBtn.selected = NO;
    btn.selected = YES;

    [UIView animateWithDuration:0.2 animations:^{
        self.slider.Width = SYRealValue(30 / 2);;
        self.slider.CenterX = btn.CenterX;
        self.selectedBtn.transform = CGAffineTransformIdentity;
        btn.transform = CGAffineTransformMakeScale(1.1, 1.1);
        btn.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(32 / 2)];
    }];
    
    self.selectedBtn = btn;
    CGFloat offsetX = btn.CenterX - self.frame.size.width * 0.5;
    if (offsetX < 0) {
        offsetX = 0;
    }
    if (offsetX > self.contentSize.width - self.frame.size.width) {
        offsetX = self.contentSize.width - self.frame.size.width;
    }
    [self setContentOffset:CGPointMake(offsetX, 0) animated:YES];
    if (self.titleChooseReturn) {
        self.titleChooseReturn(btn.tag);
    }
}

//  cache title width
- (CGFloat)widthOfTitle:(NSString *)title titleFont:(CGFloat)titleFont {
    CGSize titleSize = [title boundingRectWithSize:CGSizeMake(MAXFLOAT, _HeaderH - 2)
                                           options:NSStringDrawingUsesLineFragmentOrigin
                                        attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:titleFont] forKey:NSFontAttributeName]
                                           context:nil].size;
    return titleSize.width;
}

@end

@implementation UIView (ViewFrame)

- (void)setWidth:(CGFloat)Width {
    CGRect frame = self.frame;
    frame.size.width = Width;
    self.frame = frame;
}

- (CGFloat)Width {
    return self.frame.size.width;
}

- (void)setHeight:(CGFloat)Height {
    CGRect frame = self.frame;
    frame.size.height = Height;
    self.frame = frame;
}

- (CGFloat)Height {
    return self.frame.size.height;
}

- (void)setCenterX:(CGFloat)CenterX {
    CGPoint center = self.center;
    center.x = CenterX;
    self.center = center;
}

- (CGFloat)CenterX {
    return self.center.x;
}

- (void)setCenterY:(CGFloat)CenterY {
    CGPoint center = self.center;
    center.y = CenterY;
    self.center = center;
}

- (CGFloat)CenterY {
    return self.center.y;
}

@end
